package top.itmeans.yes.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.itmeans.yes.common.api.CommonResult;
import top.itmeans.yes.api.CommonPage;
import top.itmeans.yes.entity.OrderReturnReason;
import top.itmeans.yes.service.OrderReturnReasonService;

import java.util.List;

/**
 * <p>
 * 退货原因表 前端控制器 OMS
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/returnReason")
public class OrderReturnReasonController {
    @Autowired
    private OrderReturnReasonService orderReturnReasonService;

    /**
     * POST [host:ip/serverPath]/orderReturnReason
     */
    @ApiOperation("添加退货原因")
    @PostMapping
    public CommonResult create(@RequestBody OrderReturnReason returnReason) {
        int flag = orderReturnReasonService.create(returnReason);
        if (flag > 0) {
            return CommonResult.success(flag);
        }
        return CommonResult.failed();
    }

    /**
     * DELETE [host:ip/serverPath]/orderReturnReason
     */
    @ApiOperation("批量删除退货原因")
    @DeleteMapping
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int flag = orderReturnReasonService.delete(ids);
        if (flag > 0) {
            return CommonResult.success(flag);
        }
        return CommonResult.failed();
    }

    /**
     * PUT [host:ip/serverPath]/orderReturnReason/{id}
     */
    @ApiOperation("修改退货原因")
    @PutMapping(value = "/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody OrderReturnReason returnReason) {
        int flag = orderReturnReasonService.update(id, returnReason);
        if (flag > 0) {
            return CommonResult.success(flag);
        }
        return CommonResult.failed();
    }

    /**
     * PUT [host:ip/serverPath]/orderReturnReason/status?status=xxx&ids=xxx
     */
    @ApiOperation("修改退货原因启用状态")
    @PutMapping(value = "/status")
    public CommonResult updateStatus(@RequestParam(value = "status") Integer status,
                                     @RequestParam("ids") List<Long> ids) {
        int flag = orderReturnReasonService.updateStatus(ids, status);
        if (flag > 0) {
            return CommonResult.success(flag);
        }
        return CommonResult.failed();
    }

    /**
     * GET [host:ip/serverPath]/orderReturnReason/list
     */
    @ApiOperation("分页查询退货原因")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<OrderReturnReason>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<OrderReturnReason> reasonList = orderReturnReasonService.list(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(reasonList));
    }

    /**
     * GET [host:ip/serverPath]/orderReturnReason/{id}
     */
    @ApiOperation("获取单个退货原因详情信息")
    @GetMapping(value = "/{id}")
    public CommonResult<OrderReturnReason> getItem(@PathVariable Long id) {
        OrderReturnReason reason = orderReturnReasonService.getById(id);
        return CommonResult.success(reason);
    }
}
